<template>
  <div class="gis_wrap">
    <el-row class="GIS-container" v-loading="loading">
      <!-- 点位列表 -->
      <div class="ele_2">
        <table>
          <tr>
            <th>企业名称</th>
            <th>站点名称</th>
          </tr>
          <tr v-for="item in siteWqiList2" :key="item.value" v-if="showTabel">
            <td>{{ item.deptName }}</td>
            <td>{{ item.siteName }}</td>
          </tr>
        </table>
      </div>
      <!-- 地图 -->
      <baidu-map
        ref="map"
        class="map"
        :center="center"
        :zoom="zoom"
        @zoomend="zoomEnd"
        :scroll-wheel-zoom="true"
        :map-click="false"
        @moveend="moveEnd"
        @ready="handler"
      >

        <!-- 自定义控件-点位搜索 -->
        <bm-control
          anchor="BMAP_ANCHOR_TOP_RIGHT"
          :offset="{ width: 40, height: 20 }"
        >
          <el-select
            size="medium"
            style="width: 250px;"
            filterable
            v-model="currentPointId"
          >
            <el-option
              v-for="(item, index) in pointList"
              :key="index"
              :label="item.optionName"
              :value="item.siteId"
            >
              <el-row style="font-size: 12px;padding:0 10px;">
                {{item.optionName }}
              </el-row>
            </el-option>
          </el-select>
          <el-button
            icon="el-icon-search"
            size="medium"
            @click="focusPoint"
            type="primary"
            style="margin-left: 5px"
          ></el-button>
					<div style="text-align: center;margin-top: 20px;display: flex;justify-content: center;">
						<img src="/img/system/close.png" alt="" style="display: block;" @click="showTabel=false" v-if="showTabel"/>
						<img src="/img/system/open.png" alt="" style="display: block;" @click="showTabel=true" v-else/>
					</div>              
        </bm-control>

        <!-- 缩略图控件：anchor(位置) size(大小) -->
        <bm-overview-map
          anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
          :size="size"
          :isOpen="true"
        ></bm-overview-map>

        <!-- 点位 -->
        <div v-for="(item, index) in pointList" :key="index">
          <!-- 异常 -->
          <bm-marker
            v-if="item.factorList && !formatFactorList(item.factorList)"
            :key="index"
            :icon="{
              url: '/img/bg/tuding/icon6.png',
              size: { width: 30, height: 30 }
            }"
            :position="item.position"
            @mouseover="pointClick(item)">
          </bm-marker>
          <!-- 在用 -->
          <bm-marker
            v-else-if="item.factorList && formatFactorList(item.factorList) && item.state === '在用'"
            :key="index"
            :icon="{
              url: '/img/bg/tuding3.png',
              size: { width: 30, height: 30 }
            }"
            :position="item.position"
            @mouseover="pointClick(item)">
          </bm-marker>
          <!-- 断线 -->
          <bm-marker
            v-else-if="item.factorList && formatFactorList(item.factorList) && item.state === '断线'"
            :key="index"
            :icon="{
              url: '/img/bg/offlineMap.png',
              size: { width: 30, height: 30 }
            }"
            :position="item.position"
            @mouseover="pointClick(item)">
          </bm-marker>
        </div>
        
        <!-- 信息窗体 -->
        <bm-info-window 
          :position="infoData.position" 
          :title="infoData.deptName + '-' + infoData.siteName" 
          :show="infoShow" 
          :closeOnClick="false" 
          @clickclose="withInfoClose"
        >
          <div class="info_item">
            <div class="info_label">设备状态：</div>
            <div class="info_value">
              <span>{{ infoData.state }}</span>
            </div>
          </div>
          <div class="info-main-right" style="width:410px;margin-top: 10px;" @click="toHistory">
            <div class="right" >
              <div class="right-inner" v-for="(item, index) in infoData.factorList" :key="index">
                <div class="factor-title">
                  <span>{{ item.alias }}</span>
                  <span>({{ item.unit }})</span>
                </div>
                <div class="factor-txt">
                  <span v-if="item.isAbnormal > 0" style="font-weight: bold; color: #FF0000;">
                    {{ item.collectValue || '-' }}
                  </span>
                  <span v-else style="font-weight: bold;">
                    {{ item.collectValue || '-' }}
                  </span>	                    	
                </div>
              </div>
            </div>
          </div>				
        </bm-info-window>

      </baidu-map>
    </el-row>
  </div>
</template>

<script>
import { getGISData } from "@/api/swaq/gis";
import { getSiteWqi } from "@/api/loam/gis";
import { mapGetters } from "vuex";
import { setStore, getStore } from "@/util/store";
import { getSystemConfig} from "@/api/ficm/systemName";
export default {
  data() {
    return {
    	showTabel:true,
      // 设置center和zoom之后地图才会进行渲染
      center: "",
      size: { width: 300, height: 180 },
      zoom: 0,
      pointList: [], // 点位数组，用于渲染到地图
      infoData: {}, //  信息窗体数据
      infoShow: false, // 信息窗体显隐
      currentPointId: null, // 当前选择的点位
      dialogTitle: null, // 弹框标题
      videoVisible: false, // 弹框显隐
      loading: false,
      siteWqiList: [],
      siteWqiList2: []
    };
  },

  methods: {
    // 显示那种颜色的地图点位
    formatFactorList(arr) {
      if (arr) {
        let status = true;
        for (var i = 0; i < arr.length; i++) {
          if (arr[i].isAbnormal > 0) {
            status = false;
            break;
          }
        }
        return status;
      }
    },
    
    // 获取当前缩放比
    zoomEnd({ target }) {
      this.zoom = target.getZoom();
    },
    
    // 地图拖拽结束后触发，重新定位中心点，解决同一个点位在拖拽后无法再次定位的问题
    moveEnd({ target }) {
      this.center = target.getCenter();
    },
    
    // 跳转至综合（历史）数据页面对应的点位下
    toHistory() {
      this.$store.commit("SET_GISID", {
        value: this.infoData.siteId
      });
      this.$router.push({
        path: "/psoa31/history/historydata"
      });
    },

    // 聚焦到选择的点位
    focusPoint() {
      if (this.currentPointId != "all") {
        let status = false;
        this.siteWqiList2 = [];
        this.pointList.forEach(item => {
          if (item.siteId == this.currentPointId) {
            this.pointClick(item);
            this.center = item.position;
            status = true;
          }
        });
        if (!status) {
          this.center = "无锡";
        }
        this.siteWqiList.forEach(item => {
          if (item.siteId == this.currentPointId) {
            this.siteWqiList2.push(item);
          }
        });
      } else {
        this.withInfoClose();
        this.center = this.pointList[1].position;
        this.siteWqiList2 = this.siteWqiList;
      }
    },

    // 处理地图数据，绘制地图
    async handler() {
      this.loading = true;
			getSystemConfig().then(res=>{
				if(res.data.data.zoomLevel){
					this.zoom=res.data.data.zoomLevel
				}else{
					this.zoom=11
				}
			})      
      await getSiteWqi().then(res => {
        if (res.data.success) {
          this.siteWqiList = res.data.data;
          Object.assign(this.siteWqiList2, this.siteWqiList);
        } else {
          reject("网络拥堵，请退出重试");
        }
      });
      
      // 获取地图点位数据
      this.getMapData().then(resData => {
          this.pointList = [];
          resData.forEach(item => {
            this.siteWqiList.forEach(item2 => {
              if (item2.siteId == item.siteId) {
                this.pointList.push({
                  siteId: item.siteId,
                  deptInfoId: item.deptInfoId,
                  deptName: item.deptName, // 企业名称
                  optionName: item.deptName + "-" + item.siteName,
                  siteName: item.siteName,
                  deviceName: item.deviceName,
                  position: {
                    lng: item.logitude,
                    lat: item.latitude
                  },
                  state: item.state,
                  factorList: item.factorList,
                  wqiDayLevel: item2.wqiDayLevel
                });
              }
            });
          });
          this.loading = false;

          // 在第一项添加"全部"
          this.pointList.splice(0, 0, {
            siteName: "全部",
            optionName: "全部",
            siteId: "all"
          });
          this.currentPointId = this.pointList[0].siteId;
          this.focusPoint();
        })
        .catch(() => {
          this.loading = false;
        });
    },
    
    // 请求地图数据
    getMapData() {
      return new Promise((resolve, reject) => {
        getGISData().then(res => {
          if (res.data.success) {
            resolve(res.data.data);
          } else {
            reject("网络拥堵，请退出重试");
          }
        });
      });
    },
    
    // 显示监控，播放视频
    showVideo(point) {
      this.dialogTitle = point.siteName;
      this.videoVisible = true;
    },
    
    // 点击信息窗口关闭按钮时触发，把控制信息窗口显隐的变量置为false
    withInfoClose() {
      this.infoShow = false;
    },
    
    // 点击点位触发，展示信息窗口
    pointClick(point) {
      this.infoData = point;
      this.infoShow = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.info-main-right {
  width: 100%;
  display: flex;
  flex-direction: row;
  
  .right {
    width: 100%;
    border-top: 1px solid #ddd;
    border-left:1px solid #ddd;
    border-radius: 4px !important;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    border-top: 1px solid #ddd;
    font-size: 13px;
    background: #f1f5ff;

    .right-inner {
      display: flex;
      flex-direction: column;
      width: 33.3%;
      .factor-title {
        color: #666;
        border-bottom: 1px solid #ddd;
        border-right: 1px solid #ddd;
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        padding: 10px;
        height: 50px;
        text-align: center;
        border-radius: 0 !important;
        font-size: 12px;
      }

      .factor-txt {
        border-bottom: 1px solid #ddd;
        border-right: 1px solid #ddd;
        box-sizing: border-box;
        padding: 10px 0;
        height: 40px;
        text-align: center;
        border-radius: 0 !important;
      }
    }

    .right-inner:last-child {
      box-sizing: border-box;
    }
  }
}

.GIS-container {
  .ele_2 {
    position: absolute;
    right: 40px;
    top: 94px;
    z-index: 99;
    max-height: calc(100% - 290px);
    width: 310px;
    overflow: scroll;
    table {
      width: 100%;
      text-align: center;
      border: 1px solid #dcdfe6;
      font-size: 14px;
      border-bottom: none;
      background: rgba($color: #fff, $alpha: 0.7);
      th, td {
        font-weight: normal;
        height: 30px;
        border-bottom: 1px dashed #dcdfe6;
      }
      .item_cir {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        display: inline-block;
      }
    }
  }
  height: calc(100% - 40px);
  padding: 0 !important;
  .map {
    height: 100%;
    .info_item {
      display: flex;
      font-size: 14px;
      margin: 5px 0;
      .state_online {
        color: #009e00;
      }
      .state_offline {
        color: #8a8a8a;
      }
    }
  }
}
</style>
